<?php
class Patient_model extends Mymodel {
	public $table_name 	= "patient";
	public $table_name2     = "patient_details";
    function Patient_model()
    {
        parent::Mymodel();
    }
	
	function get_patient_data($id=NULL)
	{
		return $this->get_data($this->table_name, $id);
	}
	
	function insert_new_patient($data)
	{
		if(!$this->check_by_col($this->table_name,'code',$data['code']))
		
		return $this->insert_new_record($this->table_name , $data);
		

		return false;
	}

	function update_patient_by_id($data, $id)
	{
		$data_check=$this->get_data($this->table_name,NULL,array('code'=>$data['code']));
		
		
		if(!$this->check_by_col($this->table_name,'code',$data['code']) ||($this->check_by_col($this->table_name,'code',$data['code'])==1 && $data_check[0]['id']===$id)  )
		return $this->update_by_id($this->table_name, $data, $id);
		return false;
	}
        
	function delete_patient_by_id($id)
	{
			return $this->delete_by_id($this->table_name , $id);
	}
	
	/***** patient details ******/
	
	function insert_patient_details($data)
	{
		//NOTE :
		//Check if the patient has no id in patient_details table
		//if no id found then create new record
		//else search for patient-row and update it.
		
		$id = array('patient_id'=>$data['patient_id']);
		$is_found = $this->get_data($this->table_name2, NULL ,$id);
		//ID found
		if($is_found['count']){
			return $this->update_by_id($this->table_name2 , $data, NULL , $id);
		}
		//ID NOT Found";
		else{
			return $this->insert_new_record($this->table_name2 , $data);
		}
	}
	
	function get_patient_details($patient_id=NULL)
	{
		$id = array('patient_id'=>$patient_id);
		$data=$this->get_data($this->table_name2, NULL, $id);
		$patient=$this->get_patient_data($patient_id);
		$data[0]['code']=$patient['code'];
		//print_r($data);
		return $data;
	}
	
}
